﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuildPlannerLib.PlayerActions
{
    // Derived class of Action for moving mineral workers to gas
    public class MineralToGas : PlayerAction
    {
        // Field for the base where the worker is being moved to gas
        public Base Base;

        // Constructor with parameters for base and build
        public MineralToGas(Build build, Base b)
        {
            Build = build;
            Base = b;
            Description = "Worker to Gas (Base " + Build.Map.Bases.IndexOf(Base) + ")";
            LongDescription = "Moving a worker from Minerals to Gas at base number " + Build.Map.Bases.IndexOf(Base) + ".";
        }

        // Evaluates whether there are workers mining minerals at the base and whether the base can support more gas workers
        public override bool RequirementsFilled()
        {
            Error = null;

            if (Base.MineralWorkers == 0)
            {
                Error = "No workers assigned to minerals at the specified base.";
                return false;
            }

            if (Base.GasWorkerCapacity() == Base.GasWorkers)
            {
                if (Base.StartedGasGeysers.Sum() == Base.ConvertedGasGeysers.Sum())
                {
                    Error = "No available space in gas at the specified base.";
                    return false;
                }
            }

            return base.RequirementsFilled();
        }

        // Evaluates if there is a mineral worker in the Leaving, Close, or Idle categories and the base can currently support more gas workers
        public override bool StartCheck()
        {
            if (Base.GasWorkerCapacity() <= Base.GasWorkers)
                return false;

            if (Base.MineralWorkerStatus.Sum() == 0)
                return false;

            return base.StartCheck();
        }

        // Removes the worker from minerals and adds the worker mining gas
        public override void StartAction()
        {
            base.StartAction();

            Base.RemoveMineralWorker(ref Build);
            Base.AddGasWorker(ref Build);
        }
    }
}
